import 'package:flutter_test/flutter_test.dart';
import 'package:chewie_ohos/chewie_ohos.dart';
import 'package:video_player/video_player.dart';

void main() {

  group('ChewieController', () {
    late VideoPlayerController videoPlayerController;
    late ChewieController chewieController;

    setUp(() {
      videoPlayerController = VideoPlayerController.network(
        'https://assets.mixkit.co/videos/preview/mixkit-spinning-around-the-earth-29351-large.mp4',
      );
      chewieController = ChewieController(
        videoPlayerController: videoPlayerController,
        aspectRatio: 16 / 9,
        autoPlay: false,
        draggableProgressBar: true,
        looping: false,
        fullScreenByDefault: false,
        allowedScreenSleep: true,
        allowFullScreen: true,
        allowMuting: true,
        allowPlaybackSpeedChanging: true,
        playbackSpeeds: [0.75, 1, 1.25, 1.75, 2],
      );
    });

    tearDown(() {
      videoPlayerController.dispose();
      chewieController.dispose();
    });

    test('initial state is not full screen', () async {
      expect(chewieController.isFullScreen, false);
    });

    test('entering full screen updates state', () async {
      chewieController.enterFullScreen();
      expect(chewieController.isFullScreen, true);
    });

    test('exiting full screen updates state', () async {
      chewieController.enterFullScreen();
      chewieController.exitFullScreen();
      expect(chewieController.isFullScreen, false);
    });

    test('toggling full screen correctly toggles state', () async {
      chewieController.toggleFullScreen();
      expect(chewieController.isFullScreen, true);
      chewieController.toggleFullScreen();
      expect(chewieController.isFullScreen, false);
    });

    test('copying ChewieController retains settings', () async {
      final copiedController = chewieController.copyWith();
      expect(copiedController.allowFullScreen, chewieController.allowFullScreen);
      expect(copiedController.allowPlaybackSpeedChanging, chewieController.allowPlaybackSpeedChanging);
      expect(copiedController.playbackSpeeds, chewieController.playbackSpeeds);
    });

    test('listener is added and removed correctly', () async {
      var listenerCalled = false;
      void listener() {
        listenerCalled = true;
      }
      chewieController.addListener(listener);
      chewieController.notifyListeners();
      expect(listenerCalled, true);
      chewieController.removeListener(listener);
      listenerCalled = false;
      chewieController.notifyListeners();
      expect(listenerCalled, false);
    });
  });
}
